package com.syas.service.services;

import com.syas.base.service.services.EntityService;
import com.syas.common.exception.ValidatException;
import com.syas.common.utilities.PageList;
import com.syas.common.utilities.PageListUtil;
import com.syas.dao.interfaces.IFrmgerPfmceDao;
import com.syas.model.models.FrmgerPfmce;
import com.syas.service.interfaces.IFrmgerPfmceService;
import com.syas.service.models.FrmgerPfmceSearch;
import com.syas.web.models.FrmgerPfmceSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by weigong on 2015/11/30.
 */
@Service("FrmgerPfmceService")
@Transactional()
class FrmgerPfmceService extends EntityService<Integer, FrmgerPfmce, IFrmgerPfmceDao> implements IFrmgerPfmceService {

    @Autowired
    public FrmgerPfmceService(@Qualifier("FrmgerPfmceDao") IFrmgerPfmceDao frmgerPfmceDao) {
        super(frmgerPfmceDao);
    }

    public PageList<FrmgerPfmceSearch> listFrmgerPfmcePage(final FrmgerPfmceSearchModel frmgerPfmceSearchModel) {

        String structureHql = "new com.syas.service.models.FrmgerPfmceSearch(fp.id,fp.customerNname,fp.customerCmerEzbo," +
                "fp.headOffice,fp.branchNumber,fp.area,fp.arealeader,fp.subordinateBranch,fp.brchcomyleader,fp.salesDepartment,fp.saledeptleader," +
                "fp.subBranch,fp.diviseleader,fp.subTeam,fp.teamleader,fp.financialPlannerName,fp.financialPlannerCmerEzbo,fp.investpdt," +
                "fp.investamt,fp.achieve,fp.investtime,fp.investway,fp.effeday,fp.redetime,fp.investid,"+
                "fg.id,fg.realName,fg.ezboUname,fg.mobileNo,fg.certId,fg.level)";
        StringBuffer hqlBuffer = new StringBuffer(300);
        hqlBuffer.append("select ");
        hqlBuffer.append(structureHql);
        hqlBuffer.append("  from FrmgerPfmce as fp,Fmger as fg where fp.financialPlannerCmerEzbo = fg.ezboUname");
        if (frmgerPfmceSearchModel != null) {
            //此处加条件
            String ezboUname = frmgerPfmceSearchModel.getEzboUname();
            if (ezboUname != null && ezboUname.length() > 0) {
                hqlBuffer.append(" and fp.customerCmerEzbo = " + ezboUname + "");
            }
            String mobileNo = frmgerPfmceSearchModel.getMobileNo();
            if (mobileNo != null && mobileNo.length() > 0) {
                hqlBuffer.append(" and fg.mobileNo = " + mobileNo + "");
            }
            String certId = frmgerPfmceSearchModel.getCertId();
            if (certId != null && certId.length() > 0) {
                hqlBuffer.append(" and fg.certId = " + certId + "");
            }
        }

        String hqlBufferCount = hqlBuffer.toString().replace(structureHql, "count(*)");
        List<FrmgerPfmceSearch> items = entityDao.listFrmgerPfmceSearchPageByHql(hqlBuffer.toString(), frmgerPfmceSearchModel.getPage() - 1, frmgerPfmceSearchModel.getPageSize());
        Integer count = entityDao.countFrmgerPfmceSearchPageByHql(hqlBufferCount).intValue();
        return PageListUtil.getPageList(count, frmgerPfmceSearchModel.getPage(), items, frmgerPfmceSearchModel.getPageSize());

    }



    public boolean addFrmgerPfmceByExcel(List<FrmgerPfmce> frmgerPfmces){
        //删除之前数据

        boolean bo = true;
        try {
           // entityDao.deleteFrmgerPfmce();
            if(frmgerPfmces.size() > 0){
                for(int i = 0; i < frmgerPfmces.size(); i++){
                    FrmgerPfmce frmgerPfmce = frmgerPfmces.get(i);
                    entityDao.save(frmgerPfmce);
                }
            }else{
                bo = false;
            }
        }catch (Exception e){
            e.printStackTrace();
            bo = false;
        }
        return bo;
    }


    public FrmgerPfmce getFrmgerPfmceById(FrmgerPfmce frmgerPfmce) {
        try {
            return super.get(frmgerPfmce.getId());
        } catch (ValidatException e) {
            e.printStackTrace();
        }
        return null;
    }


}
